package net.oschina.arvin.dbjava.model;

import net.oschina.arvin.dbjava.util.Utils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 模型类
 *
 * @author Arvin
 * @time 2017/9/28 17:12
 */
public class ModelClass {

    /**
     * 包名
     */
    private String packageName;

    /**
     * model 类名称
     */
    private String name;

    /**
     * 注释
     */
    private String comment;

    /**
     * 作者
     */
    private String author;

    /**
     * 模型属性列表
     */
    private List<ModelField> modelFieldList = new ArrayList<ModelField>();

    public ModelClass(String packageName, String name, String comment) {
        this.packageName = packageName;
        this.name = name;
        this.comment = comment;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public List<ModelField> getModelFieldList() {
        return modelFieldList;
    }

    public void setModelFieldList(List<ModelField> modelFieldList) {
        this.modelFieldList = modelFieldList;
    }

    /**
     * 获取模型代码
     *
     * @return
     */
    public String toModelClassCode() {
        StringBuilder builder = new StringBuilder();
        builder.append(toPackageCode()).append("\n");
        String importCode = toImportCode();
        if (Utils.isNotBlank(importCode)) {
            builder.append(importCode).append("\n");
        }
        String commentCode = toCommentCode();
        if (Utils.isNotBlank(commentCode)) {
            builder.append(commentCode).append("\n");
        }
        builder.append(toClassDeclareCode()).append("\n");
        String fieldCode = toFieldCode();
        if (Utils.isNotBlank(fieldCode)) {
            builder.append(fieldCode).append("\n");
        }
        String getterSetterCode = toGetterSetterCode();
        if (Utils.isNotBlank(getterSetterCode)) {
            builder.append(getterSetterCode).append("\n");
        }
        builder.append("}");
        return builder.toString();
    }

    private String toClassDeclareCode() {
        return "public class " + this.name + " {\n";
    }

    private String toCommentCode() {
        String author = Utils.isBlank(this.author) ? "Gen by db-java-switch" : this.author;
        String comment = Utils.isBlank(this.comment) ? "" : this.comment;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String time = dateFormat.format(new Date());

        String commentTemplate = "/**\n" +
                " * <pre>\n" +
                " * ${comment}\n" +
                " * </pre>\n" +
                " *\n" +
                " * @author ${author}\n" +
                " * @time ${time}\n" +
                " */";
        String commentCode = commentTemplate.replaceFirst("\\$\\{comment\\}", comment);
        commentCode = commentCode.replaceFirst("\\$\\{author\\}", author);
        commentCode = commentCode.replaceFirst("\\$\\{time\\}", time);
        return commentCode;
    }

    private String toGetterSetterCode() {
        StringBuilder builder = new StringBuilder("");
        int index = 0;
        int size = modelFieldList.size();
        for (ModelField modelField : modelFieldList) {
            String getterSetterCode = modelField.toGetterSetterCode();
            if (Utils.isNotBlank(getterSetterCode)) {
                builder.append(getterSetterCode).append("\n");
                if (index < size - 1) {
                    builder.append("\n");
                }
            }
            index++;
        }
        return builder.toString();
    }

    private String toFieldCode() {
        StringBuilder builder = new StringBuilder("");
        int index = 0;
        int size = modelFieldList.size();
        for (ModelField modelField : modelFieldList) {
            String fieldCode = modelField.toFieldCode();
            if (Utils.isNotBlank(fieldCode)) {
                builder.append(fieldCode).append("\n");
                if (index < size - 1) {
                    builder.append("\n");
                }
            }
            index++;
        }
        return builder.toString();
    }

    private String toPackageCode() {
        return "package " + this.packageName + ";\n";
    }


    private String toImportCode() {
        StringBuilder builder = new StringBuilder("");
        Set<String> importedSet = new HashSet<String>();
        for (ModelField modelField : modelFieldList) {
            String importCode = modelField.toImportCode();
            if (Utils.isNotBlank(importCode)) {
                if (!importedSet.contains(importCode)) {
                    builder.append(importCode).append("\n");
                }
                importedSet.add(importCode);
            }
        }
        return builder.toString();
    }
}
